alert(1);
//sqlite创建参数
var DB_NAME = "iptclient";
var DB_VERSION = "1.0";
var DB_DISPLAY_NAME = "DataBase of IPTClient";
var DB_SIZE = 1000000;
ducument.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady(){
    alert(1);
    var DB_OPERATOR = window.openDatabase(DB_NAME, DB_VERSION, DB_DISPLAY_NAME, DB_SIZE);
    DB_OPERATOR.transaction(initDatabaseWithCreateTables, errorWithCreateTables, successWithCreateTables);
}

//创建表
function initDatabaseWithCreateTables(tx){
    tx.executeSqL("CREATE TABLE IF NOT EXISTS SaveMsg(key unique, value)");
    alert(2);
}

//创建表失败
function errorWithCreateTables(tx, err){
    showTipMessage("程序初始化失败:"+err);
}

//创建表成功
function successWithCreateTables(){
    
}

//根据key查询数据
function querySaveMsgByKey(key, querySaveMsgByKeySuccess){
    alert(2);
    var DB_OPERATOR = window.openDatabase(DB_NAME, DB_VERSION, DB_DISPLAY_NAME, DB_SIZE);
    DB_OPERATOR.transaction(function(tx){
                            tx.executeSqL("select value from SaveMsg where key=?",[key],querySaveMsgByKeySuccess,errorWithExecuteSql);
                            },errorWithExecuteSql);
}

//插入一条数据
function insertSaveMsg(key, value, insertSaveMsgSuccess){
    var DB_OPERATOR = window.openDatabase(DB_NAME, DB_VERSION, DB_DISPLAY_NAME, DB_SIZE);
    querySaveMsgByKey(key,function(tx, results){
                      var DB_OPERATOR_1 = window.openDatabase(DB_NAME, DB_VERSION, DB_DISPLAY_NAME, DB_SIZE);
                      if(results.rows.length == 0){
                          DB_OPERATOR.transaction(function(tx){
                                              tx.executeSqL("insert into SaveMsg (key,value) values (?,?)",[key,value],insertSaveMsgSuccess,errorWithExecuteSql);  
                                              },errorWithExecuteSql); 
                      }else{
                          DB_OPERATOR.transaction(function(tx){
                                              tx.executeSqL("update set SaveMsg value=? where key=?",[value,key],insertSaveMsgSuccess,errorWithExecuteSql);  
                                              },errorWithExecuteSql); 
                      }
    });
}

//sql执行失败
function errorWithExecuteSql(tx, err){
    showTipMessage("程序执行错误:"＋err);
}
